package com.wu.system_service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.wu.bean.NewsBean;
import com.wu.bean.WeiBean;
import com.wu.dao.NewsDao;
import com.wu.util.BaseUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName CollectArticle
 * @Author xwu36
 * @Date 2019/2/18 10:51
 * @Version 1.0
 * @Description
 **/
@Component
@Slf4j
public class CollectArticleService {

    @Autowired
    NewsDao dao;

    @Value("${WeixinJingXuan}")
    String url;

    @Value("${News}")
    String newsUrl;

    int times = 0;

    @Scheduled(initialDelay = 3000, fixedRate = 1000 * 1)
    public void collectArticle() throws IOException {
        times++;
        log.info("开始从聚合数据平台获取微信精选文章");
        Map map = new HashMap();
        map.put("pno", times + "");
        map.put("ps", "50");
        map.put("key", "4adca6d9fbd319c606e283c12d91523f");
        map.put("dtype", "json");
        String parm = BaseUtil.getUtil().getParm(map);
        String result = BaseUtil.getUtil().doPost(url, parm, "POST");
        ObjectMapper mapper = new ObjectMapper();
        WeiBean weiBean = mapper.readValue(result, WeiBean.class);
        System.out.println(mapper.writeValueAsString(weiBean));
        List<WeiBean.ResultBean.ListBean> list = weiBean.getResult().getList();
        SimpleDateFormat format0 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = format0.format(new Date());
        System.out.println(list.get(0).getTitle());
        for (int i = 0; i < list.size(); i++) {
            dao.addNews(list.get(i).getId(), list.get(i).getTitle(), list.get(i).getSource(), list.get(i).getUrl(), time);
        }
        map.clear();

    }
    @Scheduled(initialDelay = 3000,fixedRate = 1000*10)
    public void collectNews() throws IOException {
        times++;
        log.info("开始获取新闻");
        Map map=new HashMap();
        map.put("key","8103181164d169b8e02585dc8963e3e2");
        map.put("type","keji");
        String parm = BaseUtil.getUtil().getParm(map);
        String post = BaseUtil.getUtil().doPost(newsUrl, parm, "POST");
        ObjectMapper mapper=new ObjectMapper();
        NewsBean newsBean = mapper.readValue(post, NewsBean.class);
        System.out.println(mapper.writeValueAsString(newsBean.getResult().getData()));
        List<NewsBean.ResultBean.DataBean> data = newsBean.getResult().getData();
        for (int i = 0; i < data.size(); i++) {
            String NewsID = data.get(i).getUniquekey();
            String title = data.get(i).getTitle();
            String date = data.get(i).getDate();
            String Type = data.get(i).getCategory();
            String authorName = data.get(i).getAuthor_name();
            String url = data.get(i).getUrl();
            String pic1 = data.get(i).getThumbnail_pic_s();
            String pic2 = data.get(i).getThumbnail_pic_s02();
            String pic3 = data.get(i).getThumbnail_pic_s03();
            dao.addList(NewsID,title,date,Type,authorName,url,pic1,pic2,pic3);
        }

    }
}
